package com.mycompany.app;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.jdbc.core.simple.SimpleJdbcCall;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionInterceptor;

@Service
@Transactional()
public class AnimalService implements IAnimalService {

	Log log = LogFactory.getLog(this.getClass().getSimpleName());
	
//	private PlatformTransactionManager txManager;
	JdbcTemplate jdbcTemplate;
	SimpleJdbcTemplate simpleTemplate;
	SimpleJdbcInsert animalInsert;
	SimpleJdbcCall readAnimalCall;
//	TransactionTemplate txTemplate;
	

	public AnimalService() {
		// TODO Auto-generated constructor stub
	}

	@Autowired
	public void setDs(DataSource ds){
		System.out.println("dbg settiong ds ==========");
		jdbcTemplate = new JdbcTemplate(ds);
		simpleTemplate = new SimpleJdbcTemplate(ds);
		animalInsert = new SimpleJdbcInsert(ds).withTableName("Animal").usingGeneratedKeyColumns("id");
		readAnimalCall = new SimpleJdbcCall(ds).withProcedureName("read_animal");
	}
	/* (non-Javadoc)
	 * @see com.mycompany.app.IAnimalService#insertAnimal(com.mycompany.app.Animal)
	 */
	public int insertAnimal(Animal animal){
		log.debug("insert animal " + animal + " " + this.toString());
		simpleTemplate.update("insert into Animal (id, name, color, age, birthDate) values (:id, :name, :color, :age, :birthDate)", new BeanPropertySqlParameterSource(animal));
	//	if (1==1)
	//		throw new RuntimeException("xx");
		simpleTemplate.update("insert into Animal (id, name, color, age, birthDate) values (:id, :name, :color, :age, :birthDate)", new BeanPropertySqlParameterSource(animal));
		return animalInsert.executeAndReturnKey(new BeanPropertySqlParameterSource(animal)).intValue();
	}
	
	public void insertBatch(final List<Animal> animals){
		SqlParameterSource [] batch = SqlParameterSourceUtils.createBatch(animals.toArray());
		simpleTemplate.batchUpdate("insert into Animal (id, name, color, age, birthDate) values (:id, :name, :color, :age, :birthDate)", batch);
	}
	
	/* (non-Javadoc)
	 * @see com.mycompany.app.IAnimalService#updateAnimal(com.mycompany.app.Animal)
	 */
	public void updateAnimal(Animal animal){
		log.debug("update animal " + animal);
	}
	
	/* (non-Javadoc)
	 * @see com.mycompany.app.IAnimalService#deleteAnimal(com.mycompany.app.Animal)
	 */
	public void deleteAnimal(Animal animal, int animalId){
		log.debug("delete animal " + animal + " " + animalId);
	}
	
	/* (non-Javadoc)
	 * @see com.mycompany.app.IAnimalService#getAnimal(int)
	 */
	public Animal getAnimal(int id){
		log.debug("get animal " + id);
		return null;
	}

	@Override
	public Animal callProcedure(int id) {
		Animal animal = new Animal();
		SqlParameterSource in = new MapSqlParameterSource()
        .addValue("in_id", id); 

		Map out = readAnimalCall.execute();
		animal.setName((String) out.get("out_name"));
		animal.setAge((Integer) out.get("out_age"));
		animal.setBirthDate((Date) out.get("out_birthDate"));
		return animal;
	}
	
//	public String toString(){
//		System.out.println("AnimalService.toStringcall");
//		return "i am animalservice";
//	}
}
